#include "transferfromqthread.h"
#include <QThread>
#include <QDebug>
#include "./collectiongrabwindow.h"
#include <QFile>
#include <QDir>

TransferFromQThread::TransferFromQThread(linkItem item,QObject* par): QThread(par)
  ,m_runCount(20)
{
    m_item=item;
}

TransferFromQThread::~TransferFromQThread()
{
    qDebug() << "TransferFromQThread::~TransferFromQThread()";
}

void TransferFromQThread::setSomething()
{
    msleep(500);

    //QString str = QString("%1->%2,thread id:%3").arg(__FUNCTION__).arg(__FILE__).arg((int)QThread::currentThreadId());
    //emit message(str);
}

void TransferFromQThread::getSomething()
{
    msleep(500);
    //emit message(QString("%1->%2,thread id:%3").arg(__FUNCTION__).arg(__FILE__).arg((int)QThread::currentThreadId()));
}

void TransferFromQThread::setRunCount(int count)
{
    m_runCount = count;
    //emit message(QString("%1->%2,thread id:%3").arg(__FUNCTION__).arg(__FILE__).arg((int)QThread::currentThreadId()));
}

void TransferFromQThread::run()
{
    if(m_item.binType==BINTYPE_FILE)
        transferFile();
    else if(m_item.binType==BINTYPE_TEAMVIEW)
    {
        transferPixmap();
    }

}

void TransferFromQThread::doSomething()
{
    msleep(500);
    //emit message(QString("%1->%2,thread id:%3").arg(__FUNCTION__).arg(__FILE__).arg((int)QThread::currentThreadId()));
}
static pthread_mutex_t mutex_lockPixmap;//发送锁
void TransferFromQThread::transferPixmap()
{
    wsimBIN_head binHead;
    binHead.tofd=m_item.tfd;
    binHead.magic=m_item.magic;
    binHead.binType=m_item.binType;
    const char*  ch;
    ch=m_item.label.data();
    strncpy(binHead.label,ch,255);

    collectionGrabWindow*pix=&(MY_COLLECTION_GRABWINDOW);
    if(pix!=NULL){
        QByteArray byteArray;
        pthread_mutex_init(&mutex_lockPixmap,NULL);
   while(true){


        if(!pix->isCollection())
        {
            binHead.index=0;            
            WSIM_client::sendBYTE(binHead,NULL,0);
            break;
        }
        pthread_mutex_lock(&mutex_lockPixmap);
        byteArray.resize(0);

        if(pix->toMinByte07(byteArray)){
        if(byteArray.size()>0)
        {
            binHead.index=1;
            char* pbuf=byteArray.data();
            int maxPageSize=1024*15;
            //qDebug()<<"总大小"<<byteArray.size();
            int sendedSize=0;//已发
            while(true)
            {
                if(sendedSize>=byteArray.size()){
                    //qDebug()<<"发完一个";
                    break;//发完
                }
                char* p=&pbuf[sendedSize];
                int ysize=byteArray.size()-sendedSize;//余
                if(ysize>maxPageSize)
                {
                    binHead.dataLen=maxPageSize;
                }
                else
                    binHead.dataLen=ysize;

                WSIM_client::sendBYTE(binHead,p,binHead.dataLen);
                //qDebug()<<"发送"<<binHead.index<<"大小"<<binHead.dataLen;
                sendedSize+=binHead.dataLen;
                WSIM_client::delayms(50);
                emit progress(binHead.tofd,0,binHead.magic,binHead.index);
                binHead.index++;
            }
           WSIM_client::delayms(70);
        }
        else
            qDebug()<<"没有采到数据";
        }
        else
            qDebug()<<"采集错误";
        pthread_mutex_unlock(&mutex_lockPixmap);
    }
    pthread_mutex_destroy(&mutex_lockPixmap);//销毁
   }
   //QString msg=QString("“%1”传送完成，请到个人目录下查看（其它自定义目录或文件名)。").arg(QString::fromStdString(m_item.label));
   //WSIM_client::sendTEXT_fd(m_item.tfd,msg.toStdString());
}
void TransferFromQThread::transferFile()
{
  QFile *m_localFile=new QFile(QString::fromStdString(m_item.filename));
      if(!m_localFile->open(QFile::ReadOnly)){
          qDebug()<<"打开传送文件出错，"<<QString::fromStdString(m_item.filename);
          return;
      }
      qint64 m_totalBytes=m_localFile->size();


      wsimBIN_head binHead;
      binHead.tofd=m_item.tfd;
      binHead.magic=m_item.magic;
      binHead.binType=m_item.binType;
      const char*  ch;
      ch=m_item.label.data();
      strncpy(binHead.label,ch,255);


      binHead.index=1;
      QByteArray m_outBlock;


       int pageByte=m_item.maxPageLen;
      while(true)
      {
          if(m_totalBytes>0)
          {
              m_outBlock=m_localFile->read(pageByte);
          }
          else
              break;
          if(m_outBlock.size()>0)
          {

            WSIM_client::sendBYTE(binHead,m_outBlock.data(),m_outBlock.size());
            WSIM_client::delayms(100);


              //WSIM_client::delayms(50);
              emit progress(binHead.tofd,0,binHead.magic,binHead.index);
              //qDebug()<<"向"<<binHead.tofd<<"传送"<<binHead.label<<"序号"<<binHead.index<<"大小"<<m_outBlock.size();
              binHead.index++;


          }
          else
              break;

          if(m_outBlock.size()<(pageByte))
              break;

          m_outBlock.resize(0);

      }

      m_localFile->close();

      QString msg=QString("“%1”传送完成，请到个人目录下查看（其它自定义目录或文件名)。").arg(QString::fromStdString(m_item.label));
      WSIM_client::sendTEXT_fd(m_item.tfd,msg.toStdString());
}
